      integer itmp(12),itmp0(12)
      character*200 line
      real tmp(12)
C**** add Ant-data (unit 1) to v2-data (unit 2); new file: unit 12
C**** assumption is that IDs are ordered numerically low->high in both
C****               input files

      call getarg(1,line)
      open(1,file=line,form='formatted')
      call getarg(2,line)
      open(2,file=line,form='formatted')
      open(12,file='v2.meanx',form='formatted')

      read(1,'(a)') line                        ! read from new sources
!                     CountryCode  ID year data
      call readl(line,   icc0,    id0,iyr0,tmp)

   20 read(2,'(i3,i9,i4,12i5)',end=200) icc,id,iyr,itmp     ! read v2
C**** copy v2-source data from earlier stations or same station
   25 if(icc.lt.icc0.or.(icc.eq.icc0.and.id.le.id0)) then
         write (12,'(i3,i9.9,i4,12i5)') icc,id,iyr,itmp !
         iccp=icc
         idp=id
         go to 20
      end if

C**** merge in station data from (presumably) new source
      if(icc0.eq.iccp.and.idp.eq.id0) then  ! should not happen
         write(*,*) 'ID already used',icc0,id0
         stop 4
      end if
   35 write (12,'(i3,i9.9,i4,12i5)') icc0,id0,iyr0,
     *                                  (nint(10.*tmp(i)),i=1,12)
      icc0p=icc0
      id0p=id0
      read(1,'(a)',end=100) line                  ! read new data
      call readl(line,   icc0,    id0,iyr0,tmp)
      if(icc0.eq.icc0p.and.id0.eq.id0p) go to 35
      go to 25  ! to compare to current v2-station

c**** No more new data - copy data for remaining v2-data
  100 write(12,'(i3,i9.9,i4,12i5)') icc,id,iyr,itmp
      read(2,'(i3,i9,i4,12i5)',end=300) icc,id,iyr,itmp
      go to 100

c**** No more v2 data - copy remaining new data
  200 write (12,'(i3,i9.9,i4,12i5)') icc0,id0,iyr0,
     *                                  (nint(10.*tmp(i)),i=1,12)
      read(1,'(i3,i9,i4,12i5)',end=300) icc0,id0,iyr0,tmp
      read(1,'(a)',end=300) line
      call readl(line,   icc0,    id0,iyr0,tmp)
      go to 200

  300 stop
      end

      subroutine readl (line,icc,id,iyr,tmp)
C**** reads T-data from standard 12f8.1 tables with missing=-999.9 or
C**** from non-standard tables with blanks for missing data, whose
C****      fields are 7 characters wide and start at columns 19 or 20;
C****      so data='       ' or ' xxxx.x', but not all blank !
      character line*200,blank*7/'       '/
      real tmp(12)

      read(line,'(i3,i9,i4)') icc,id,iyr
      if(line(23:23).eq.'.') then              ! standard tables
         read(line(17:200),'(12f8.1)') tmp
         return
      end if

C**** Non-standard tables
C**** check whether data are present and how they are lined up
      np=index(line,'.')
      if(np.le.0) then
        write(*,*) 'non-data line encountered:',line
        stop 3
      end if

      n1=16+mod(np,7) ! position of first data field
      do m=1,12
        tmp(m)=-999.9 ! if field is blank
        if(line(n1:n1+6).ne.blank) read(line(n1:n1+6),'(f7.1)') tmp(m)
        n1=n1+7
      end do

      return
      end
